/***************************************************************************
 *   Copyright (C) 2007 by Marcin Swiechowicz   *
 *   marcin.swiechowicz@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "kdcvideocodecdelegate.h"

#include <kdclib.h>
#include <kdcdisc.h>

#include <QModelIndex>

#include <KComboBox>

KDCVideoCodecDelegate::KDCVideoCodecDelegate( QObject *parent )
	: QItemDelegate( parent ), p_movie( 0 )
{

}

void KDCVideoCodecDelegate::setCurrentData( const KDCMovie *movie )
{
	p_movie = movie;
}

QWidget* KDCVideoCodecDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
	Q_UNUSED( option );
	Q_UNUSED( index );
	KComboBox *editor = new KComboBox( parent );
	KDC::DiscType type = p_movie->structure()->disc()->type();
	if ( type == KDC::DVD )
	{
		editor->addItem( KDCLib::toString( KDC::MPEG1 ), KDC::MPEG1 );
		editor->addItem( KDCLib::toString( KDC::MPEG2 ), KDC::MPEG2 );
	}
	else
		if ( type == KDC::VCD )
			editor->addItem( KDCLib::toString( KDC::MPEG1 ), KDC::MPEG1 );
	else
		editor->addItem( KDCLib::toString( KDC::MPEG2 ), KDC::MPEG2 );
	editor->setCurrentIndex( editor->findData( p_movie->videoStream()->codec() ) );
	connect( editor, SIGNAL( currentIndexChanged( int ) ), this, SLOT( slotCurrentIndexChanged( int ) ) );
	return editor;
}

void KDCVideoCodecDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex& index ) const
{
	KComboBox *tmp = static_cast<KComboBox*>( editor );
	model->setData( index, tmp->currentText() );
}

void KDCVideoCodecDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
	Q_UNUSED( index );
	editor->setGeometry( option.rect );
}

void KDCVideoCodecDelegate::slotCurrentIndexChanged( int index )
{
	KComboBox *editor = qobject_cast<KComboBox*>( sender() );
	if ( editor )
	{
		if ( editor->currentText() == KDCLib::toString( KDC::MPEG1 ) )
			emit codecChanged( KDC::MPEG1 );
		else
			emit codecChanged( KDC::MPEG2 );
	}
}
